# coding=utf8
import json
from app import create_app
from app.modules.common.jsonFileUtil import getJsonFile, saveJsonFile
from app.extensions import db
from app.modules.common.utils import DbCommit
from app.modules.entrance.models import EntranceInfo
from app.modules.apps.models import AppInfo

application = create_app(flask_config = 'local')


def main(_env_name, _zone_code, _tasks, _ignore_list):
    with application.app_context():
        for _task in _tasks:
            if _task['name'] in _ignore_list:
                continue

            if _task['type'] != 'tomcat' and _task['type'] != 'php':
                continue

            _type = None
            _info = AppInfo(app_name = _task['name'], host = _task['ip'], current_status = "GNONE",
                            expect_status = "GNONE", zone_code = _zone_code, group = _task['group'],
                            env_name = _env_name)
            if _task['type'] == 'tomcat':
                _type = 'jmx'
                _info.jmx_port = '10001'
                _info.app_type = "java"
            elif _task['type'] == 'php':
                _type = 'hosts'
                _info.app_type = "php"
                _info.agent_port = None

            _entrance = EntranceInfo.query.filter_by(type = _type, env_name = _env_name, zone_code = _zone_code).first()
            _info.entrance_id = _entrance.id
            _info.entrance_type = _type

            db.session.add(_info)
            print _info.to_dict()
        DbCommit()


def updateApp(_env_name, _zone_code, _tasks):
    with application.app_context():
        for _task in _tasks:
            _info = AppInfo.query.filter_by(app_name = _task['name'], host = _task['ip'], zone_code = _zone_code,
                                            group = _task['group'], env_name = _env_name).first()
            if _info is None:
                continue

            _info.http_port = _task['httpPort']
            print _info.to_dict()
            # DbCommit()
    pass


if __name__ == '__main__':
    _env_name = 'online'

    _ignore_list = ['mms', 'upush', 'uworkflow', 'upush2-console-ui', 'packing-alg', 'uschedule']

    _file = "/Users/wangjian/Documents/online.json.bak"

    _json = getJsonFile(_file)
    _areas = _json['areas']

    with application.app_context():
        for _area in _areas:
            _zone_code = _area['zone_code']
            if _zone_code is not None and _zone_code != '':
                _tasks = _area['tasks']
                for _task in _tasks:
                    _info = AppInfo.query.filter_by(app_name = _task['name'], host = _task['ip'],
                                                    zone_code = _zone_code, group = _task['group'],
                                                    env_name = _env_name, http_port = _task['httpPort']).first()
                    if _task['name'] in _ignore_list:
                        _task['is_resource'] = '1'
                    else:
                        _task['is_resource'] = '0'
                    if _task['type'] == 'tomcat' or _task['type'] == 'spring_boot_jar':
                        if _info is not None:
                            _task['jmx_port'] = _info.jmx_port
                        else:
                            _task['jmx_port'] = '10001'
                    else:
                        _task['jmx_port'] = ''
                    if _task['type'] == 'php':
                        if _info is not None:
                            _task['agent_port'] = _info.agent_port
                        else:
                            _task['agent_port'] = '7777'
                    else:
                        _task['agent_port'] = ''
                    pass
                        # main(_env_name, _zone_code, _tasks, _ignore_list)
                        # updateApp(_env_name, _zone_code, _tasks)
    saveJsonFile(_file, _json)
    pass
